﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FrameworkLib.Facades;
using GestionePolveri2.DAL;

namespace GestionePolveriLib.Facades
{
    public class OrdersDatesFacade : FacadeBase<GestionePolveriEntities>
    {
        GestionePolveriEntities context = null;

        public OrdersDatesFacade()
        {
            context = this.GetContext();
        }

        public bool CheckOpenOrder(int idPolvere)
        {
            bool retValue = false;

            try
            {
                //var obj = from x in this.context.OrdersDates where x.IDPolvere == idPolvere && !x.IsClosed select x;
                var obj = from x in this.context.OrdersDates where x.IDPolvere == idPolvere && (!x.OrderCloseDate.HasValue || x.IsGeneratedByCorrection) select x;
                if (obj.Any())
                {
                    retValue = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return retValue;
        }

        public bool SetOrderDate(int idPolvere)
        {
            bool retValue = false;

            try
            {
                if (!CheckOpenOrder(idPolvere))
                {
                    OrdersDates od = new OrdersDates();
                    od.IDPolvere = idPolvere;
                    od.OrderDate = DateTime.Now;

                    this.context.OrdersDates.AddObject(od);
                    this.context.SaveChanges();

                    retValue = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return retValue;
        }

        public bool CancelOrderDate(int idPolvere)
        {
            bool retValue = false;

            try
            {
                var obj = from x in context.OrdersDates where x.IDPolvere == idPolvere select x;
                if (obj.Any())
                {
                    this.context.OrdersDates.DeleteObject(obj.FirstOrDefault());
                    this.context.SaveChanges();

                    retValue = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return retValue;
        }

        public bool SetOrderCloseDate(int idPolvere)
        {
            bool retValue = false;

            try
            {
                var obj = from x in context.OrdersDates orderby x.OrderDate descending where x.IDPolvere == idPolvere && !x.OrderCloseDate.HasValue select x;

                if (obj.Any())
                {
                    OrdersDates od = obj.FirstOrDefault();
                    od.OrderCloseDate = DateTime.Now;
                    od.IsGeneratedByCorrection = false;

                    this.context.SaveChanges();

                    retValue = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return retValue;
        }

        public bool SetOrderCloseDateByCorrection(int idPolvere)
        {
            bool retValue = false;

            try
            {
                var obj = from x in context.OrdersDates orderby x.OrderDate descending where x.IDPolvere == idPolvere && !x.OrderCloseDate.HasValue select x;

                if (obj.Any())
                {
                    OrdersDates od = obj.FirstOrDefault();
                    od.OrderCloseDate = DateTime.Now;
                    od.IsGeneratedByCorrection = true;

                    this.context.SaveChanges();

                    retValue = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return retValue;
        }
    }
}
